﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text.RegularExpressions;

namespace Hosting.Class
{
    public class Orders
    {

        public Orders() { }

        #region [Get all Orders]
        /// <summary>
        /// Get all Orders
        /// </summary>
        /// <returns>
        /// <para>Return all Orders</para>
        /// <para>null  : error</para>
        /// </returns>
        public List<PC_Order_GetAllResult> getAll()
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var list = db.PC_Order_GetAll().ToList();
                return list;
            }
            catch(Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Get one Order]
        /// <summary>
        /// Get one Orders
        /// </summary>
        /// <param name="id">ID of Order</param>
        /// <returns>
        /// <para>Return one Order</para>
        /// <para>null  : error</para>
        /// </returns>
        public List<PC_Order_GetOneResult> getOne(string id)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var list = db.PC_Order_GetOne(Int32.Parse(id)).ToList();
                return list;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        #endregion

        #region [Insert Order]
        /// <summary>
        /// Insert Order
        /// </summary>
        /// <param name="clientName">Name of customer</param>
        /// <param name="clientEmail">Email of customer</param>
        /// <param name="clientGender">Gender of customer</param>
        /// <param name="clientPhone">Phone of customer</param>
        /// <param name="clientAddress">Address of customer</param>
        /// <param name="isDone">Check Order is finished or not</param>
        /// <returns>
        /// <para>0     : success</para>
        /// <para>-99   : error</para>
        /// </returns>
        public int add(string clientName, string clientEmail, string clientGender, string clientPhone, string clientAddress, bool isDone)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isInsert = db.PC_Order_Ins(clientName, clientEmail, clientGender, clientPhone, clientAddress, isDone);
                return isInsert;
            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion

        #region [Check Insert is valid or not]
        /// <summary>
        /// Insert Order
        /// </summary>
        /// <param name="clientName">Name of customer</param>
        /// <param name="clientEmail">Email of customer</param>
        /// <param name="clientPhone">Phone of customer</param>
        /// <param name="clientAddress">Address of customer</param>
        /// <returns>
        /// <para>1     : success</para>
        /// <para>10    : Customer name is null or empty</para>
        /// <para>20    : Customer email is null or empty</para>
        /// <para>21    : Customer email is not valid</para>
        /// <para>30    : Customer phone is null or empty</para>
        /// <para>31    : Customer phone is not valid</para>
        /// <para>40    : Customer address is null or empty</para>
        /// </returns>
        public int addValid(string clientName, string clientEmail, string clientPhone, string clientAddress)
        {
            if (String.IsNullOrEmpty(clientName))
            {
                return 10;
            }
            else if (String.IsNullOrEmpty(clientEmail))
            {
                return 20;
            }

            Regex rEmailValid = new Regex(@"^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*@[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+${0,100}");
            Match mEmailValid = rEmailValid.Match(clientEmail);
            if (!mEmailValid.Success)
            {
                return 21;
            }
            else if (String.IsNullOrEmpty(clientPhone))
            {
                return 30;
            }

            Regex rPhoneValid = new Regex(@"^[0-9]{10,15}$");
            Match mPhoneValid = rPhoneValid.Match(clientPhone);
            if (!mPhoneValid.Success)
            {
                return 31;
            }
            else if (String.IsNullOrEmpty(clientAddress))
            {
                return 40;
            }

            return 1;
        }
        #endregion

        #region [Update Order]
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">ID of Order</param>
        /// <param name="clientName">Name of customer</param>
        /// <param name="clientEmail">Email of customer</param>
        /// <param name="clientGender">Gender of customer</param>
        /// <param name="clientPhone">Phone of customer</param>
        /// <param name="clientAddress">Address of customer</param>
        /// <param name="isDone">Check Order is finished or not</param>
        /// <returns>
        /// <para>1     : successfully</para>
        /// <para>0     : This order has been done, cannot edit this order anymore!</para>
        /// <para>-99   : error</para>
        /// </returns>
        public int edit(string id, string clientName, string clientEmail, string clientGender, string clientPhone, string clientAddress, bool isDone)
        {
            try
            {
                int? isUpdate = null;
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                var isEdit = db.PC_Order_Upd(Int32.Parse(id), clientName, clientEmail, clientGender, clientPhone, clientAddress, isDone, ref isUpdate);
                if (isUpdate.Value == 1)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }

            }
            catch (Exception ex)
            {
                return -99;
            }
        }
        #endregion

        #region [Check Update is valid or not]
        /// <summary>
        /// Check Update is valid or not
        /// </summary>
        /// <param name="id">ID of Order</param>
        /// <param name="clientName">Name of customer</param>
        /// <param name="clientEmail">Email of customer</param>
        /// <param name="clientPhone">Phone of customer</param>
        /// <param name="clientAddress">Address of customer</param>
        /// <returns>
        /// <para>1     : success</para>
        /// <para>2     : Order ID is null or empty</para>
        /// <para>3     : Order ID is not valid</para>
        /// <para>10    : Customer name is null or empty</para>
        /// <para>20    : Customer email is null or empty</para>
        /// <para>21    : Customer email is not valid</para>
        /// <para>30    : Customer phone is null or empty</para>
        /// <para>31    : Customer phone is not valid</para>
        /// <para>40    : Customer address is null or empty</para>
        /// </returns>
        public int editValid(string id, string clientName, string clientEmail, string clientPhone, string clientAddress)
        {
            if (String.IsNullOrEmpty(id))
            {
                return 2;
            }

            Regex rArtileId = new Regex("^[0-9]{0,}$");
            Match mArtileId = rArtileId.Match(id);
            if (!mArtileId.Success)
            {
                return 3;
            }
            else if (String.IsNullOrEmpty(clientName))
            {
                return 10;
            }
            else if (String.IsNullOrEmpty(clientEmail))
            {
                return 20;
            }

            Regex rEmailValid = new Regex(@"^[-!#$%&'*+/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z{|}~])*@[a-zA-Z](-?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+${0,100}");
            Match mEmailValid = rEmailValid.Match(clientEmail);
            if (!mEmailValid.Success)
            {
                return 21;
            }
            else if (String.IsNullOrEmpty(clientPhone))
            {
                return 30;
            }

            Regex rPhoneValid = new Regex(@"^[0-9]{10,15}$");
            Match mPhoneValid = rPhoneValid.Match(clientPhone);
            if (!mPhoneValid.Success)
            {
                return 31;
            }
            else if (String.IsNullOrEmpty(clientAddress))
            {
                return 40;
            }

            return 1;
        }
        #endregion

        #region [Delete an existed Order]
        /// <summary>
        /// Delete an existed Order
        /// </summary>
        /// <param name="id">ID of Order</param>
        /// <returns>
        /// <para>1     : success</para>
        /// <para>0     : Order is done, cannot delete</para>
        /// <para>-99   : error</para>
        /// </returns>
        public int delete(string id)
        {
            try
            {
                HostingDomainWebsiteDataContext db = new HostingDomainWebsiteDataContext();
                int? isDelete = 0;
                var isSuccess = db.PC_Order_Del(Int32.Parse(id), ref isDelete);
                if (isDelete.Value == 1)
                {
                    return 1;
                }
                else
                {
                    return 0;
                }
            }
            catch(Exception ex)
            {
                return -99;
            }
        }
        #endregion
    }
}